#include "Core.h"
#include "cstdio"
using namespace std;
void LineAnalyser::AutoAnalyse(wxTextFile& file)
{
    //freopen("key.txt","w",stdout);
    keys.clear();
   // int cnt = 0;
  //  wxMessageBox(file.GetName());
  //  printf("select file %s\n",file.GetName());
    for(wxString line=file.GetFirstLine();!file.Eof();line=file.GetNextLine())
    {
       // printf("%d %s\n",cnt++,line.c_str());
        //wxMessageBox(line);
        int pos = line.Find(_(":"));
        if(pos>=0&&pos<11)
        {
            keys.insert(make_pair(line.SubString(0,pos-1),KeyInfo::Random()));
            //continue;
        }
        pos = line.Find(_("："));
        if(pos>=0&&pos<11)
        {
            keys.insert(make_pair(line.SubString(0,pos-1),KeyInfo::Random()));
        }
    }
}
void LineAnalyser::Clear()
{
    keys.clear();
}
void LineAnalyser::Add(const wxString& s,const KeyInfo& info)
{
    keys.insert(make_pair(s,info));
}
void LineAnalyser::TransForm(wxTextFile& in,wxTextFile& out)
{
    out.Clear();
    out.AddLine(_("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">"));
    out.AddLine(_("<html>"));
    out.AddLine(_("<head>"));
    out.AddLine(_("<style type=\"text/css\">"));
    out.AddLine(_("body {font-weight:bold;white-space:normal;}"));
    for(map<wxString,KeyInfo>::iterator it=keys.begin();it!=keys.end();++it)
    {
       // out.AddLine(_("\r\n<!--")+it->first+_("-->"));
        out.AddLine(it->second.ToHtmlStyleTag());
    }
    out.AddLine(_("</style>"));
    out.AddLine(_("</head>"));
    out.AddLine(_("<body>"));
    wxString curTag = _("<span>");
    for(wxString line=in.GetFirstLine();!in.Eof();line=in.GetNextLine())
    {
        if(line.Len()==0)
        {
            curTag = _("<span>");
            continue;
        }
        int pos = line.Find(_(":"));
        KeyInfo* kf = NULL;
        map<wxString,KeyInfo>::iterator it;
        if(pos!=wxNOT_FOUND)
        {
            const wxString& s = line.SubString(0,pos-1);
            it = keys.find(s);
            if(it!=keys.end()) kf = &it->second;
        }
        pos = line.Find(_("："));
        if(pos!=wxNOT_FOUND)
        {
            const wxString& s = line.SubString(0,pos-1);
            it = keys.find(s);
            if(it!=keys.end()) kf = &it->second;
        }
        if(kf) curTag = wxString::Format(_("<span class=\"cv%d\">"),kf->id);
        out.AddLine(curTag);
        out.AddLine(line);
        out.AddLine(_("</span></br></br>"));
    }
    out.AddLine(_("</body>"));
    out.AddLine(_("</html>"));
    out.Write(out.GuessType());
}
